﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using HTDAL.HTSPC_DBDataSetTableAdapters;
using System.Configuration;

namespace HTspcAppProject
{
    public partial class History : Form
    {
        private GraphPane myPane;
        //PointPairList list_room_set;
        //PointPairList list_oil_set;
        //PointPairList list_cp_set;
        PointPairList list_room_real;
        PointPairList list_oil_real;
        PointPairList list_cp_real;
        PointPairList list_cmv;

        LineItem myCurve;
        LineItem myCurve2;
        LineItem myCurve3;
        LineItem myCurve4;

        //用于绘制垂直于x轴的线
        PointPairList list_mouse;
        //用于绘制当前选中的点
        PointPairList now_point;
        //当前选中点的坐标
        PointPair point_mouse;

        double ymax = Convert.ToDouble(ConfigurationManager.AppSettings["ymax"]);
        double ymin = Convert.ToDouble(ConfigurationManager.AppSettings["ymin"]);

        ht_pmlogTableAdapter pmlogTA = new ht_pmlogTableAdapter();

        int pointIndex;
        string DevNm;
        public History(int device_num,string devNm)
        {
            InitializeComponent();

            for (int i = 0; i < 24; i++)
            {
                cbx1H.Items.Add(i);
                cbx2H.Items.Add(i);
            }

            for (int i=0; i < 60; i++)
            {
                cbx1M.Items.Add(i);
                cbx1S.Items.Add(i);
                cbx2M.Items.Add(i);
                cbx2S.Items.Add(i);
            }
            
                this.DevNm = devNm;
            list_room_real = new PointPairList();
            list_oil_real = new PointPairList();
            list_cp_real = new PointPairList();
            list_cmv = new PointPairList();
            list_mouse = new PointPairList();
            now_point = new PointPairList();
            point_mouse = new PointPair();

            label_title.Location = new Point(this.Width / 2 - label_title.Width / 2, 10);
            label_title.Text = devNm + "历史曲线显示(Furnace History Curve)";
            //label_num.Text = "#" + device_num;

            myPane = zedgraphControl.GraphPane;
            myPane.Title.Text = "运行工艺曲线(Performance Curve)";
            myPane.XAxis.Title.Text = "时间(Time)";
            myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.Title.IsVisible = false;
            myPane.YAxis.Scale.IsVisible = false;

            myPane.XAxis.Type = AxisType.Date;
            myPane.XAxis.Scale.Format = "yy-MM-dd HH:mm:ss";
            zedgraphControl.PointDateFormat = "HH:mm:ss";

            DateTime begin_time = DateTime.Now;

            //分别添加数据显示
            myCurve = myPane.AddCurve("后室温度",
                  list_room_real, Color.Red, SymbolType.None);

            myCurve2 = myPane.AddCurve("油槽温度",
                  list_oil_real, Color.Blue, SymbolType.None);

            myCurve3 = myPane.AddCurve("CP值",
                  list_cp_real, Color.Black, SymbolType.None);

            myCurve4 = myPane.AddCurve("CMV阀开度",
                  list_cmv, Color.Yellow, SymbolType.None);

            /* ************************************** 新增代码 10-11 ***************************************** */
            LineItem myCurve_mouse = myPane.AddCurve("",
                  list_mouse, Color.Black, SymbolType.None);

            LineItem myCurve_nowPoint = myPane.AddCurve("", 
                  now_point, Color.Red,SymbolType.Diamond);

            myCurve_mouse.Line.Width = 1;
            myPane.XAxis.MajorGrid.IsVisible = true;
            
            myCurve.Line.Width = CommonClass.HlineW;
            myCurve2.Line.Width = CommonClass.HlineW;
            myCurve3.Line.Width = CommonClass.HlineW;
            myCurve4.Line.Width = CommonClass.HlineW;

            zedgraphControl.IsShowPointValues = true;
            //myPane.YAxis.Scale.Min = CommonClass;
            //myPane.YAxis.Scale.Max = 1000;
            /* ************************************** 新增代码 10-11 ***************************************** */
            // 当坐标系变化时，需要调用该函数进行刷新
            zedgraphControl.AxisChange();
            cbx1H.Text = DateTime.Now.Hour.ToString();
            cbx2H.Text = DateTime.Now.Hour.ToString();
            cbx1M.Text = DateTime.Now.Minute.ToString();
            cbx2M.Text = DateTime.Now.Minute.ToString();
            cbx1S.Text = DateTime.Now.Second.ToString();
            cbx2S.Text = DateTime.Now.Second.ToString();
        }

        public History(int device_num,string devNm,PointPairList list_room, PointPairList list_oil, PointPairList list_cp, PointPairList cmv)
        {
            InitializeComponent();
            this.DevNm = devNm;

            for (int i = 0; i < 24; i++)
            {
                cbx1H.Items.Add(i);
                cbx2H.Items.Add(i);
            }

            for (int i = 0; i < 60; i++)
            {
                cbx1M.Items.Add(i);
                cbx1S.Items.Add(i);
                cbx2M.Items.Add(i);
                cbx2S.Items.Add(i);
            }

            list_room_real = new PointPairList();
            list_oil_real = new PointPairList();
            list_cp_real = new PointPairList();
            list_cmv = new PointPairList();

            /* *************************Change****************************** */
            list_room_real = list_room;
            list_oil_real = list_oil;
            list_cp_real = list_cp;
            list_cmv = cmv;

            list_mouse = new PointPairList();
            now_point = new PointPairList();
            point_mouse = new PointPair();

            label_title.Location = new Point(this.Width / 2 - label_title.Width / 2, 10);
            label_title.Text = device_num + "号加热炉历史曲线显示(Furnace History Curve)";
            //label_num.Text = "#" + device_num;

            myPane = zedgraphControl.GraphPane;
            myPane.Title.Text = "运行工艺曲线(Performance Curve)";
            myPane.XAxis.Title.Text = "时间(Time)";
            //myPane.YAxis.Title.Text = "温度/值(Temp)";

            myPane.XAxis.Type = AxisType.Date;
            myPane.XAxis.Scale.Format = "yy/MM/dd HH:mm:ss";
            zedgraphControl.PointDateFormat = "HH:mm:ss";
            myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.Title.IsVisible = false;
            myPane.YAxis.Scale.IsVisible = false;

            DateTime begin_time = DateTime.Now;

            //分别添加数据显示
            myCurve = myPane.AddCurve("后室温度",
                  list_room_real, Color.Plum, SymbolType.None);

            myCurve2 = myPane.AddCurve("油槽温度",
                  list_oil_real, Color.Blue, SymbolType.None);

            myCurve3 = myPane.AddCurve("CP值",
                  list_cp_real, Color.Black, SymbolType.None);

            myCurve4 = myPane.AddCurve("CMV阀开度",
                  list_cmv, Color.Yellow, SymbolType.None);

            /* ************************************** 新增代码 10-11 ***************************************** */
            LineItem myCurve_mouse = myPane.AddCurve("",
                  list_mouse, Color.Black, SymbolType.None);

            LineItem myCurve_nowPoint = myPane.AddCurve("",
                  now_point, Color.Red, SymbolType.Diamond);

            myCurve_mouse.Line.Width = 1;
            myPane.XAxis.MajorGrid.IsVisible = true;

            myCurve.Line.Width = CommonClass.HlineW; ;
            myCurve2.Line.Width = CommonClass.HlineW; ;
            myCurve3.Line.Width = CommonClass.HlineW; ;
            myCurve4.Line.Width = CommonClass.HlineW; ;

            zedgraphControl.IsShowPointValues = true;
            //myPane.YAxis.Scale.Min = -10;
            //myPane.YAxis.Scale.Max = 1000;
            /* ************************************** 新增代码 10-11 ***************************************** */
            // 当坐标系变化时，需要调用该函数进行刷新
            zedgraphControl.AxisChange();
            zedgraphControl.Refresh();
            if (list_room.Count > 0)
            {
                cbx1H.Text = ((XDate)list_room[0].X).DateTime.Hour.ToString();
                cbx2H.Text = ((XDate)list_room.Last().X).DateTime.Hour.ToString();
                cbx1M.Text = ((XDate)list_room[0].X).DateTime.Minute.ToString();
                cbx2M.Text = ((XDate)list_room.Last().X).DateTime.Minute.ToString();
                cbx1S.Text = ((XDate)list_room[0].X).DateTime.Second.ToString();
                cbx2S.Text = ((XDate)list_room.Last().X).DateTime.Second.ToString();
            }
            else
            {
                cbx1H.Text = DateTime.Now.Hour.ToString();
                cbx2H.Text = DateTime.Now.Hour.ToString();
                cbx1M.Text = DateTime.Now.Minute.ToString();
                cbx2M.Text = DateTime.Now.Minute.ToString();
                cbx1S.Text = DateTime.Now.Second.ToString();
                cbx2S.Text = DateTime.Now.Second.ToString();
            }
        }

        private void time_begin_Click(object sender, EventArgs e)
        {
            //time_begin.Text = "";
        }

        private void time_end_Click(object sender, EventArgs e)
        {
            //time_end.Text = "";
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void History_SizeChanged(object sender, EventArgs e)
        {
            label_title.Location = new Point(this.Width / 2 - label_title.Width / 2, 10);
        }
        /* ************************************** 新增代码 10-11 ***************************************** */
        private void button1_Click(object sender, EventArgs e)
        {
            //点击搜索按钮后，进行搜索和结果显示
            DateTime begin = Convert.ToDateTime(dateTimePicker1.Value.ToString("yyyy/MM/dd") + " " + cbx1H.Text + ":" + cbx1M.Text + ":" + cbx1S.Text);
            DateTime end = Convert.ToDateTime(dateTimePicker2.Value.ToString("yyyy/MM/dd") + " " + cbx2H.Text + ":" + cbx2M.Text + ":" + cbx2S.Text);
            //调用数据库查询，传递参数 begin, end
            //再调用下列画图函数
            list_cp_real.Clear();
            list_room_real.Clear();
            list_oil_real.Clear();
            list_cmv.Clear();
            textBox_x.Text = "";
            hswdlab.Text = "";
            ycwdlab.Text = "";
            cplab.Text = "";
            cmvlab.Text = "";
            float room_temp, oil_temp, cp, cmv;
            DateTime dt;
            ht_pmlogTableAdapter pmTa = new ht_pmlogTableAdapter();
            DataTable pmLogDt = pmTa.GetDataByTime(begin, end, DevNm);
            foreach (DataRow dr in pmLogDt.Rows)
            {
                room_temp = Convert.ToSingle(dr["hswdCur"]);
                oil_temp = Convert.ToSingle(dr["ycwdCur"]);
                cp = Convert.ToSingle(dr["cpCur"]);
                cmv = Convert.ToSingle(dr["CMV"]);
                dt = Convert.ToDateTime(dr["dt"]);
                HistoryAdd((XDate)dt, room_temp, oil_temp, cp, cmv);

            }
            zedgraphControl.AxisChange();
            zedgraphControl.Refresh();
            ymax = myPane.YAxis.Scale.Max;
            ymin = myPane.YAxis.Scale.Min;
            /********************************* 测试添加数据 *******************************/
            
            
            //Random ran=new Random();
            //room_temp = (float)ran.Next(0,1000);
            //oil_temp = (float)ran.Next(0,300);
            //cp = (float)ran.Next(0,20);
            //cmv = (float)((ran.Next(0, 10)) / 10.0);


            /********************************* 测试添加数据 *******************************/

        }

        private void HistoryAdd(XDate time, float room_temp_real, float oil_temp_real, float cp_value_real, float cmv_value)
        {
            XDate now = time;
            double add_time = (double)now;

            //对数据进行预处理，使得显示更加美观
            DataModify(ref room_temp_real, ref oil_temp_real, ref cp_value_real, ref cmv_value);

            list_room_real.Add(add_time, Convert.ToDouble(room_temp_real));
            list_oil_real.Add(add_time, Convert.ToDouble(oil_temp_real));
            list_cp_real.Add(add_time, Convert.ToDouble(cp_value_real));
            list_cmv.Add(add_time, Convert.ToDouble(cmv_value));


        }

        private void DataModify(ref float room_temp_real,ref float oil_temp_real,ref  float cp_value_real, ref float cmv_value)
        {
            //room:0-1000  oil:0-300  cp: 0-20  cmv: 1
            room_temp_real = room_temp_real * Convert.ToSingle(CommonClass.hsy);
            oil_temp_real = oil_temp_real * Convert.ToSingle(CommonClass.ycy);
            cp_value_real = cp_value_real * Convert.ToSingle(CommonClass.cpy);
            cmv_value = cmv_value * Convert.ToSingle(CommonClass.cmvy);
         }

        private string zedgraphControl_PointValueEvent(ZedGraphControl sender, GraphPane pane, CurveItem curve, int iPt)
        {
            pointIndex = iPt;
            if (curve.Label.Text == "")
                return default(string);

            list_mouse.Clear();
            now_point.Clear();

            XDate pointX = new XDate(curve.Points[iPt].X);

            textBox_x.Text = pointX.DateTime.ToLongTimeString();
            hswdlab.Text = (myCurve.Points[iPt].Y / Convert.ToSingle(CommonClass.hsy)).ToString("N2");
            ycwdlab.Text = (myCurve2.Points[iPt].Y / Convert.ToSingle(CommonClass.ycy)).ToString("N2");
            cplab.Text = (myCurve3.Points[iPt].Y / Convert.ToSingle(CommonClass.cpy)).ToString("N2");
            cmvlab.Text = (myCurve4.Points[iPt].Y / Convert.ToSingle(CommonClass.cmvy)).ToString("N2");
            //垂直于x轴的线
            list_mouse.Add(curve.Points[iPt].X, ymin);
            list_mouse.Add(curve.Points[iPt].X, ymax);

            point_mouse.X = curve.Points[iPt].X;
            point_mouse.Y = curve.Points[iPt].Y;

            now_point.Add(point_mouse);

            zedgraphControl.AxisChange();
            zedgraphControl.Refresh();

            return default(string);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // 将当前选中的点坐标加上指定的周期值
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                textBox1.Text = "1";
            }
            double period = double.Parse(textBox1.Text);
            //if (textBox1.Text == "")
            //    period = 1;

            XDate pointX = new XDate(point_mouse.X);
            pointX.AddSeconds(period);
            point_mouse.X = (double)pointX;

            now_point.Clear();
            now_point.Add(point_mouse);

            list_mouse.Clear();
            list_mouse.Add(new PointPair(pointX, ymin));
            list_mouse.Add(new PointPair(pointX, ymax));

            zedgraphControl.Refresh();

            //按x时间，查询数据库。根据返回的数据更新界面上的几个textbox的值
            //PointPair pp= list_room_real.Single(x => x.X == (double)pointX);
            textBox_x.Text = pointX.DateTime.ToLongTimeString();
            DataTable pointDt = pmlogTA.GetDataByATime(pointX.DateTime);
            if (pointDt.Rows.Count > 0)
            {
                hswdlab.Text = Convert.ToDouble(pointDt.Rows[0]["hswdCur"]).ToString("N2");
                ycwdlab.Text = Convert.ToDouble(pointDt.Rows[0]["ycwdCur"]).ToString("N2");
                cplab.Text = Convert.ToDouble(pointDt.Rows[0]["cpCur"]).ToString("N2");
                cmvlab.Text = Convert.ToDouble(pointDt.Rows[0]["CMV"]).ToString("N2");
            }
            else
            {
                hswdlab.Text = "无";
                ycwdlab.Text = "无";
                cplab.Text = "无";
                cmvlab.Text = "无";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //查询是否存在该值，若存在，绘制
            // 将当前选中的点坐标减去指定的周期值
            double period = double.Parse(textBox1.Text);
            if (textBox1.Text == "")
                period = 1;

            XDate pointX = new XDate(point_mouse.X);
            pointX.AddSeconds(0 - period);
            point_mouse.X = (double)pointX;

            //此时，point_mouse为加上或减去指定周期的新坐标值
            now_point.Clear();
            now_point.Add(point_mouse);

            list_mouse.Clear();


            list_mouse.Add(new PointPair(pointX, ymin));
            list_mouse.Add(new PointPair(pointX, ymax));

            zedgraphControl.Refresh();
            //按x时间，查询数据库。根据返回的数据更新界面上的几个textbox的值
            textBox_x.Text = pointX.DateTime.ToLongTimeString();
            DataTable pointDt = pmlogTA.GetDataByATime(pointX.DateTime);
            if (pointDt.Rows.Count > 0)
            {
                hswdlab.Text = Convert.ToDouble(pointDt.Rows[0]["hswdCur"]).ToString("N2");
                ycwdlab.Text = Convert.ToDouble(pointDt.Rows[0]["ycwdCur"]).ToString("N2");
                cplab.Text = Convert.ToDouble(pointDt.Rows[0]["cpCur"]).ToString("N2");
                cmvlab.Text = Convert.ToDouble(pointDt.Rows[0]["CMV"]).ToString("N2");
            }
            else
            {
                hswdlab.Text = "无";
                ycwdlab.Text = "无";
                cplab.Text = "无";
                cmvlab.Text = "无";
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void cbx1H_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void History_Load(object sender, EventArgs e)
        {

        }
        /* ************************************** 新增代码 10-11 ***************************************** */
    }
}
